Skip to content

Commit

Permalink
smart cache-control headers for media fetch
Browse files Browse the repository at this point in the history
The HTTP Cache-Control header output now respects the Dokuwiki media parameters
'cache', 'recache', and 'nocache'.
  - cache: cachetime or one hour (which ever is larger)
  - recache: remaining cachetime + 10 seconds so the newly recached media is used
  - nocache: no caching

Notes:
  - 'proxy-revalidate' should ensure that public caches always revalidate, this
    will keep caches from (accidentally) ignoring Dokuwiki's ACL rules.
  - 'no-transform' should keep media from being modified in transit by caches.

Impact:
Speeds up page layout/display by browsers when they already have the media
in their [memory] cache. For example the 2nd time a page is viewed. This
also reduces server load by eliminating most of the overhead needed to
return '304 Not Modified' responses each time an image is viewed.

darcs-hash:20060421025424-05dcb-23ff26c5cb410bcd166299a840f4c500fa0d112e.gz
  • Loading branch information
Ben Coburn committed Apr 21, 2006
1 parent 4f3c496 commit 8373015
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lib/exe/fetch.php
Expand Up @@ -70,23 +70,42 @@
}

// finally send the file to the client
sendFile($FILE,$MIME);
sendFile($FILE,$MIME,$CACHE);

/* ------------------------------------------------------------------------ */

/**
* Set headers and send the file to the client
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Ben Coburn <btcoburn@silicodon.net>
*/
function sendFile($file,$mime){
function sendFile($file,$mime,$cache){
global $conf;
$fmtime = filemtime($file);
// send headers
header("Content-Type: $mime");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// smart http caching headers
if ($cache==-1) {
// cache
// cachetime or one hour
header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
header('Pragma: public');
} else if ($cache>0) {
// recache
// remaining cachetime + 10 seconds so the newly recached media is used
header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
header('Pragma: public');
} else if ($cache==0) {
// nocache
header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
header('Pragma: public');
}
header('Accept-Ranges: bytes');
//send important headers first, script stops here if '304 Not Modified' response
http_conditionalRequest(filemtime($file));
http_conditionalRequest($fmtime);
list($start,$len) = http_rangeRequest(filesize($file));

//application mime type is downloadable
Expand Down

0 comments on commit 8373015

Please sign in to comment.